<?php
	//Initial date: 2009-05-31
	//Last change: 2009-05-31 by Kamil Jaskiewicz
	
	include("classes/DBTable.class.php");
	include("classes/tasks.class.php"); 
	include("classes/projects.class.php"); 
	include("classes/contacts.class.php");

	$module_name = "Tasks";
	$action = $_REQUEST['action'];
	$tasks = new tasks();

	if ($_REQUEST['submit'])
	{
		$id		= $_REQUEST['id'];
		$t_name = $_REQUEST['task_name'];
		$t_desc	= $_REQUEST['task_description'];
		$t_p_id	= $_REQUEST['task_project_id'];
		$t_s	= $_REQUEST['status'];
		$t_pc	= $_REQUEST['task_percent_complete'];
		$t_t	= $_REQUEST['type'];

		if ($action == 'edit')
		{
			$tasks->DB->query("UPDATE tasks SET task_name='$t_name', task_description='$t_desc', 
				task_project_id='$t_p_id', task_status='$t_s', 
				task_percent_complete='$t_pc', task_type='$t_t' WHERE task_id='$id'");

		} 
		else if ($action == 'add')
		{
			$q = "INSERT INTO tasks VALUES ('', '$t_name', '$t_desc', '$t_p_id', '$t_s', '$t_pc', '$t_t')";
			$tasks->DB->query($q);			
			$id = $tasks->DB->LastID;
		}
		else if($action == 'add_contact')
		{
			$c_id = $_REQUEST['contact_id'];	
			$tasks->DB->query("INSERT INTO task_contacts VALUES('$c_id', '$id')");
		}
		header("location:tasks.php?action=view&id=$id");
		exit();
	}

	if ($action == 'delete')
	{
		$id = $_REQUEST['id'];
		if($id)
		{
			$tasks->delete($id);
			header("location:tasks.php");
			exit();
		}

		$contact_id = $_REQUEST['contact_id'];
		if ($contact_id )
		{
			$t_id = $_REQUEST['t_id'];
			$tasks->deleteContactForTask($contact_id, $t_id);
			header("location:tasks.php?action=view&id=$t_id");
			exit();
		}
	}


	include("header.php");
	

	//action not defined, display all tasks
	if (!$action)
	{
		$tasks->selectAll();

		print("<table id='grid'>");
		print("<tr><td><b>Task</b></td><td><b>Project</b></td><td colspan='3'></td></tr>\n");
		while($tasks->getNext())
		{
			print("<tr>");
			print("<td><a href='tasks.php?action=view&id=$tasks->task_id'>".str_replace(" ", "&nbsp;", $tasks->task_name)."</a></td>");
			print("<td>".str_replace(" ", "&nbsp;", $tasks->project_name)."</td>");
			print("<td><a href='tasks.php?action=edit&id=$tasks->task_id'>Edit</a></td>");
			print("<td><a href='tasks.php?action=delete&id=$tasks->task_id' onclick='return confirmation(\"Are you sure you want to delete this task?\");' >Delete</a></td>");
			print("<td style='width:80%'>&nbsp;</td>");
			print("</tr>\n\n");
		}
		print("<tr><td colspan='5' style='text-align:right'><a href='tasks.php?action=add'>Add new task</a></td></tr>");
		print("</table>");
	}


	//display details of the task
	if ($action == 'view')
	{
		$id=$_REQUEST['id'];
		$tasks->findID($id);

		print("<table id='grid'>");
		print("<tr><td><b>Name</b></td><td>$tasks->task_name</td></tr>\n");
		print("<tr><td><b>Description</b></td><td>$tasks->task_description</td></tr>\n");
		print("<tr><td><b>Project</b></td><td>$tasks->project_name</td></tr>\n");
		print("<tr><td><b>Status</b></td><td>$tasks->status</td></tr>\n");
		print("<tr><td><b>Percent Complete</b></td><td>$tasks->task_percent_complete</td></tr>\n");
		print("<tr><td><b>Type</b></td><td>$tasks->type</td></tr>\n");
		print("<tr><td colspan='2'><a href='tasks.php?action=edit&id=$tasks->task_id'>Edit</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='tasks.php?action=delete&id=$tasks->task_id' onclick='return confirmation(\"Are you sure you want to delete this task?\");' >Delete</a></td></tr>\n");
		print("</table>");

		$tasks->selectContactsForTask($id);
		print("<h3>Contacts working on task</h3>");
		print("<table id='grid'>");
		print("<tr><td><b>Name</b></td><td></td></tr>\n");
		while($tasks->getNext())
		{
			print("<tr>");
			print("<td>$tasks->contact_name</td>");
			print("<td><a href='tasks.php?action=delete&contact_id=$tasks->contact_id&t_id=$id'>Delete</a></td>");
			print("</tr>\n\n");
		}
		print("<tr><td colspan='2'><a href='tasks.php?action=add_contact&id=$id'>Add contact</a></td></tr>\n");
		print("</table>");

	}

	if ($action == 'add_contact')
	{
		$id = $_REQUEST['id'];
		print("<form action='tasks.php' method='post'>");		
		print("<input type='hidden' name='id' value='$id' />");
		print("<input type='hidden' name='action' value='add_contact' />");
		print("<input type='hidden' name='submit' value='true' />");
		print("<b>Contact: </b>");
		print("<select name='contact_id'>\n");
		$contact = new contacts();
		$contact->selectAll();
		while($contact->getNext())
		{
			print("<option value='$contact->contact_id'>$contact->contact_first_name $contact->contact_last_name</option>\n");
		}
		print ("</select>\n");		
		print ("<p></p><input type='submit' value='Submit' />&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='Cancel' onclick='history.back();' />");
		print("</form>");
	}

	if ($action == 'edit' || $action == 'add')
	{
		$id = $_REQUEST['id'];
		if ($id)
			$tasks->findID($id);
		$task = new tasks();
		$project = new projects();
		print("<form action='tasks.php' method='post'>");
		if ($action == 'edit')
		{
			print("<input type='hidden' name='action' value='edit' />");
			print("<input type='hidden' name='id' value='$id' />");
		}
		else if ($action == 'add')
		{
			print("<input type='hidden' name='action' value='add' />");
		}
		print("<input type='hidden' name='submit' value='true' />");
		print("<table id='grid'>");
		print("<tr><td><b>Name</b></td><td><input type='text' value='$tasks->task_name' name='task_name'/></td></tr>\n");
		print("<tr><td><b>Description</b></td><td><textarea name='task_description'>$tasks->task_description</textarea></td></tr>\n");
		
		print("<tr><td><b>Project</b></td><td><select name='task_project_id'>\n");
		$project->selectAll();
		while($project->getNext())
		{
			if ($project->project_id != $tasks->task_project_id)
				print("<option value='$project->project_id'>$project->project_name</option>\n");
			else
				print("<option selected value='$project->project_id'>$project->project_name</option>\n");
		}
		print ("</select></td></tr>\n");

		print("<tr><td><b>Status</b></td><td><select name='status'>\n");
		$task->getPossibleStatusValues();
		while($task->getNext())
		{
			if ($task->dict_id != $tasks->task_status)
				print("<option value='$task->dict_id'>$task->name</option>\n");
			else
				print("<option selected value='$task->dict_id'>$task->name</option>\n");
		}
		print ("</select></td></tr>\n");
		print("<tr><td><b>Percent Complete</b></td><td><input type='text' value='$tasks->task_percent_complete' name='task_percent_complete'/></td></tr>\n");

		print("<tr><td><b>Type</b></td><td><select name='type'>");

		$task->getPossibleTypeValues();
		while($task->getNext())
		{
			if ($task->dict_id != $tasks->task_type)
				print("<option value='$task->dict_id'>$task->name</option>\n");
			else
				print("<option selected value='$task->dict_id'>$task->name</option>\n");
		}		
		print("</select></td></tr>\n");

		print("<tr><td colspan='2'><input type='submit' value='Submit' />&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='Cancel' onclick='history.back();' /></td></tr>\n");
		print("</table>");
		print("</form>");
	}
	
	include("footer.php");
?>
